import 'package:flame/components/component.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:hello_flutter/demo/game/game_controller.dart';

class ScoreTxT extends Component {
  final GameController controller;
  TextPainter textPainter;
  Offset position;

  ScoreTxT(this.controller) {
    textPainter = TextPainter(
      textAlign: TextAlign.center,
      textDirection: TextDirection.ltr,
    );
    position = Offset.zero;
  }
  @override
  void render(Canvas canvas) {
    textPainter.paint(canvas, position);
  }

  @override
  void update(double time) {
    if ((textPainter.text ?? '') != '${controller.score}') {
      textPainter.text = TextSpan(
        text: '${controller.score}',
        style: TextStyle(color: Colors.black, fontSize: 64),
      );
      textPainter.layout();
      position = Offset(
        (controller.screenSize.width / 2) - (textPainter.width / 2),
        (controller.screenSize.height * 0.2) - (textPainter.height / 2),
      );
    }
  }
}
